package org.ssssssss.curd.strategy;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.ssssssss.curd.exception.MagicApiCurdGenerateException;
import org.ssssssss.curd.pojo.TableColumn;
import org.ssssssss.curd.pojo.TableMeta;
import org.ssssssss.curd.util.MagicApiCurdSpringUtil;
import org.ssssssss.magicapi.datasource.model.DataSourceInfo;
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;

import java.util.List;

/**
 * 全局路由获取表元信息策略
 */
public class RouteGetTableMetaStrategy implements GetTableMetaStrategy, InitializingBean {

    /**
     * magic-api 动态数据源对象
     */
    private MagicDynamicDataSource magicDynamicDataSource;
    /**
     * 获取表元信息策略
     */
    private GetTableMetaStrategy getTableMetaStrategy;
    /**
     * magic-api 数据源对象
     */
    private DataSourceInfo dataSourceInfo;

    /**
     * 新建一个 {@link RouteGetTableMetaStrategy} 对象
     * @param dataSourceInfo magic-api 数据源对象
     */
    public RouteGetTableMetaStrategy(DataSourceInfo dataSourceInfo) {
        this.dataSourceInfo=dataSourceInfo;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //根据 dataSourceInfo 获取数据源信息
        MagicDynamicDataSource.DataSourceNode dataSource = magicDynamicDataSource.getDataSource(dataSourceInfo.getKey());
        //根据数据源的驱动信息找到适合的  GetTableMetaStrategy 对象
        String driverClassName = dataSourceInfo.getDriverClassName();
        if(StringUtils.isEmpty(driverClassName)){
            throw new MagicApiCurdGenerateException(" no found driverClassName,dataSourceInfo = "+dataSourceInfo.getKey());
        }
        switch (driverClassName){
            //mysql
            case "com.mysql.jdbc.Driver":
            case "com.mysql.cj.jdbc.Driver":
                getTableMetaStrategy= MagicApiCurdSpringUtil.getBean(MysqlGetTableMetaStrategy.class,dataSource);
                break;
            //oracle
            case "oracle.jdbc.driver.OracleDriver":
                break;
            //postgresql
            case "org.postgresql.Driver":
                break;
            //sqlserver
            case "com.microsoft.sqlserver.jdbc.SQLServerDriver":
                break;
            //db2
            case "com.ibm.db2.jcc.DB2Driver":
                break;
            default:
                throw new MagicApiCurdGenerateException(" no support driverClassName = "+dataSourceInfo.getDriverClassName()+",dataSourceInfo = "+dataSourceInfo.getKey());
        }
    }

    @Override
    public List<TableColumn> listTableColumns(String tableName) {
        return getTableMetaStrategy.listTableColumns(tableName);
    }

    @Override
    public List<TableMeta> listTables() {
        return getTableMetaStrategy.listTables();
    }

    public MagicDynamicDataSource getMagicDynamicDataSource() {
        return magicDynamicDataSource;
    }

    public void setMagicDynamicDataSource(MagicDynamicDataSource magicDynamicDataSource) {
        this.magicDynamicDataSource = magicDynamicDataSource;
    }


}
